home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
IRIX 6.2 Applications 1996 May
/
SGI IRIX 6.2 Applications 1996 May.iso
/
dist
/
impr_dev.idb
/
usr
/
impressario
/
src
/
drivers
/
laserjetPJL
/
pclpjl.c.z
/
pclpjl.c
Wrap
C/C++ Source or Header
|
1996-05-06
|
25KB
|
898 lines
/**************************************************************************
*
* Copyright (c) 1992 Silicon Graphics, Inc.
* All Rights Reserved
*
* THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF SGI
*
* The copyright notice above does not evidence any actual of intended
* publication of such source code, and is an unpublished work by Silicon
* Graphics, Inc. This material contains CONFIDENTIAL INFORMATION that is
* the property of Silicon Graphics, Inc. Any use, duplication or
* disclosure not specifically authorized by Silicon Graphics is strictly
* prohibited.
*
* RESTRICTED RIGHTS LEGEND:
*
* Use, duplication or disclosure by the Government is subject to
* restrictions as set forth in subdivision (c)(1)(ii) of the Rights in
* Technical Data and Computer Software clause at DFARS 52.227-7013,
* and/or in similar or successor clauses in the FAR, DOD or NASA FAR
* Supplement. Unpublished - rights reserved under the Copyright Laws of
* the United States. Contractor is SILICON GRAPHICS, INC., 2011 N.
* Shoreline Blvd., Mountain View, CA 94039-7311
**************************************************************************
*
* File: pclpjl.c
*
* Summary:
* PCL/PJL utility library. Contains routines to generate common
* PCL amd PJL commands.
*
* Description and guidelines:
*
* Set of routines that know how to drive HP PCL printers. Written for
* the HP laserjet models 4Plus and newer.
*
* PCL functions start with 'pcl'. PJL functions start with 'pjl'
* PJL functions called after PCL functions (typically).
* Not designed as a set of isolation routines -- caller needs to
* call in correct order and should understand the internals before
* using.
*
* All functions return LJ_EXIT_OK if no error; else an error code from
* pclpjl.h is returned;
*
* ferror should be used by the caller if the returned error is
* LJ_WRITE_STREAM_ERR.
*
* Routines that do not start with 'pcl' or 'pjl' should not be
* used outside of this file.
*
* See Also:
* Hp's PCL and PJL manuals
*
**************************************************************************/
#ident "$Revision: 1.5 $"
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <bstring.h>
#include <math.h>
#include "laserjetPJL.h"
#include "pclpjl.h"
/* Header string for each line to send to printer */
#define HEADER_LEN 40
/* Model number in use. Should be set by caller with escSetModel before
* calling other functions. Allows some functions to be slightly different
* if a particular model requries it.
*/
static int Model = LJ_MODEL_DEFAULT;
/* Tells us if we have set the compression mode */
static int CompressionSet = -1;
/* Once in ratser mode we set InRasterMode so we can get out if job aborted. */
static boolean InRasterMode = FALSE;
static boolean NoPJL = FALSE;
static boolean Debug = FALSE;
static int HorizResolution = DEFAULT_RESOLUTION;
static int VertResolution = DEFAULT_RESOLUTION;
/**************************************************************************/
int pjlsetNoPJL(boolean noPJL)
{
NoPJL = noPJL;
return (LJ_EXIT_OK);
}
/**************************************************************************/
int pjlSetModel(char *modelName)
{
/* Call first, used by all routines if they need to do something
* device specific. Compares modelName to known model names in the
* pclpjl.h file.
*/
int ii;
Model = LJ_MODEL_DEFAULT;
for (ii=0; ii<LJ_NUM_MODELS; ii++) {
if (!(strcasecmp(modelName,LJ_MODEL_NAMES[ii]))) {
Model = ii;
break;
}
}
if (Debug) {
fprintf(stderr,"pjlSetModel: model=%s\n",modelName);
fprintf(stderr,"pjlSetModel: Model=%i\n",Model);
}
return (LJ_EXIT_OK);
}
/**************************************************************************/
int pjlGetModel()
{
/* Supply model to caller in case caller does something device specific */
if (Debug) fprintf(stderr,"pjlGetModel called. Model=%i\n",Model);
return(Model);
}
/**************************************************************************/
int pjlEnterPJLMode(FILE *out)
{
/* Enter PJL mode by sending UEL */
if (Debug) fprintf(stderr,"pjlEnterPJLMode called\n");
if (NoPJL) return (LJ_EXIT_OK);
if (fprintf(out,"\033%%-12345X@PJL\n")<0) return(LJ_WRITE_STREAM_ERR);
if (fflush(out) != 0) return (LJ_WRITE_STREAM_ERR);
return (LJ_EXIT_OK);
}
/**************************************************************************/
int pjlResetPrinter(FILE *out)
{
/* Normally called at end of job to reset all the printer
* settings set with @PJL SET commands back to their front
* panel defaults.
*/
if (Debug) fprintf(stderr,"pjlResetPrinter called\n");
if (NoPJL) return (LJ_EXIT_OK);
if (fprintf(out,"@PJL RESET\r\n") < 0) return(LJ_WRITE_STREAM_ERR);
if (fflush(out) != 0) return (LJ_WRITE_STREAM_ERR);
return (LJ_EXIT_OK);
}
/**************************************************************************/
int pjlEnterPCLMode(FILE *out)
{
/* Enter PCL mode, anything sent after this should be PCL
* (but that is not enforced by these routines). Normally an
* <esc>E is sent right after this. See pclResetPrinter.
*/
if (Debug) fprintf(stderr,"pjlEnterPCLMode called\n");
if (NoPJL) return (LJ_EXIT_OK);
if (fprintf(out,"@PJL ENTER LANGUAGE=PCL\n") < 0) {
return(LJ_WRITE_STREAM_ERR);
}
if (fflush(out) != 0) return (LJ_WRITE_STREAM_ERR);
return (LJ_EXIT_OK);
}
/**************************************************************************/
int pjlSetCopies(FILE *out, int copies)
{
/* Set number of copies. Only check is that it is greater then 1 */
if (Debug) fprintf(stderr,"pjlSetCopies called. copies=%i\n",copies);
if (NoPJL) return (LJ_EXIT_OK);
if (copies>1) {
if (fprintf(out,"@PJL SET COPIES=%i\n",copies) < 0) {
return(LJ_WRITE_STREAM_ERR);
}
}
return (LJ_EXIT_OK);
}
/**************************************************************************/
int pjlSetPageProtect(FILE *out, boolean onoff)
{
/* You almost always want this turned off so the printer would not have
* to load a full page before printing it.
*/
/*
* Model Notes:
* 4V Ignored (allow user to set on front panel)
* 4P Ignored (allow user to set on front panel)
* 4PLUS Ignored (allow user to set on front panel)
* 4SI Ignored (allow user to set on front panel)
* 5L Supports ON, OFF, AUTO (use OFF over AUTO)
* 5P Supports ON, OFF, AUTO (use OFF over AUTO)
* 5SI Ignored (allow user to set on front panel)
*/
if (Debug) fprintf(stderr,"pjlSetPageProtect called. onoff=%i\n",onoff);
if (NoPJL) return (LJ_EXIT_OK);
if ((Model == LJ_MODEL_5P) || (Model == LJ_MODEL_5L)) {
if (onoff) {
if (fprintf(out,"@PJL SET PAGEPROTECT=ON\n") < 0) {
return(LJ_WRITE_STREAM_ERR);
}
} else {
if (fprintf(out,"@PJL SET PAGEPROTECT=OFF\n") < 0) {
return(LJ_WRITE_STREAM_ERR);
}
}
}
return (LJ_EXIT_OK);
}
/**************************************************************************/
int pjlSetRes(FILE *out, int xres, int yres)
{
/* Note that setting resolution in PJL does not set the resolution for
* pcl raster graphics. See pclSetRes.
*/
if (Debug) {
fprintf(stderr,"pjlSetRes called. xres=%i, yres=%i\n",xres,yres);
}
if (NoPJL) return (LJ_EXIT_OK);
HorizResolution = xres;
VertResolution = yres;
if (fprintf(out,"@PJL SET RESOLUTION=%i\n",xres) < 0) {
return(LJ_WRITE_STREAM_ERR);
}
return (LJ_EXIT_OK);
}
/**************************************************************************/
int pjlSetPaperSize(FILE *out, int size)
{
/* Supported paper size varies by device. Check is not done
* to insure device supports requested paper size.
*/
if (Debug) fprintf(stderr,"pjlSetPaperSize called. Size=%i\n",size);
if (NoPJL) return (LJ_EXIT_OK);
/* A 4P can jam if you tell it one paper size and another is loaded.
* So, don't set it in software -- user must set on front panel. We
* still want user to set it on GUI so text2ps can create page breaks
* at correct location so it is still available to the user.
*/
if (Model == LJ_MODEL_4P) return (LJ_EXIT_OK);
if (fprintf(out,"@PJL SET PAPER=") < 0) return(LJ_WRITE_STREAM_ERR);
switch (size) {
case -999:
if (Debug) fprintf(stderr,"pjlSetPaperSize size is CUSTOM\n");
if (fprintf(out,"CUSTOM\n") < 0) return(LJ_WRITE_STREAM_ERR);
break;
case PD_SIZE_LEGAL:
if (Debug) fprintf(stderr,"pjlSetPaperSize size is LEGAL\n");
if (fprintf(out,"LEGAL\n") < 0) return(LJ_WRITE_STREAM_ERR);
break;
case PD_SIZE_B:
if (Debug) fprintf(stderr,"pjlSetPaperSize size is LEDGER\n");
if (fprintf(out,"LEDGER\n") < 0) return(LJ_WRITE_STREAM_ERR);
break;
case PD_SIZE_A4:
if (Debug) fprintf(stderr,"pjlSetPaperSize size is A4\n");
if (fprintf(out,"A4\n") < 0) return(LJ_WRITE_STREAM_ERR);
break;
case PD_SIZE_A3:
if (Debug) fprintf(stderr,"pjlSetPaperSize size is A3\n");
if (fprintf(out,"A3\n") < 0) return(LJ_WRITE_STREAM_ERR);
break;
case PD_SIZE_EXEC:
if (Debug) fprintf(stderr,"pjlSetPaperSize size is EXECUTIVE\n");
if (fprintf(out,"EXECUTIVE\n") < 0) return(LJ_WRITE_STREAM_ERR);
break;
case PD_SIZE_LETTER:
default:
if (Debug) fprintf(stderr,"pjlSetPaperSize size is LETTER\n");
if (fprintf(out,"LETTER\n") < 0) return(LJ_WRITE_STREAM_ERR);
break;
}
return (LJ_EXIT_OK);
}
/**************************************************************************/
int pjlSetDuplex(FILE *out, int duplex)
{
/* Only send this if the device supports duplexor. We only send the PJL
* command if duplex is turned on.
*/
/*
* Model Notes:
* 4V Ignored
* 4P Ignored
* 4PLUS Supported (optional)
* 4SI Supported (optional)
* 5L Ignored
* 5P Ignored
* 5SI Supported (optional)
*/
if (Debug) fprintf(stderr,"pjlSetDuplexcalled. duplex=%i\n",duplex);
if (NoPJL) return (LJ_EXIT_OK);
if (duplex == LJ_DUPLEX_OFF) return (LJ_EXIT_OK);
if (fprintf(out,"@PJL SET DUPLEX=ON\n") < 0) return(LJ_WRITE_STREAM_ERR);
if (fprintf(out,"@PJL SET BINDING=") < 0) return(LJ_WRITE_STREAM_ERR);
if (duplex == LJ_DUPLEX_SHORTSIDE) {
if (fprintf(out,"SHORTEDGE\n") < 0) return(LJ_WRITE_STREAM_ERR);
} else {
if (fprintf(out,"LONGEDGE\n") < 0) return(LJ_WRITE_STREAM_ERR);
}
return (LJ_EXIT_OK);
}
/**************************************************************************/
int pjlSetOutBin(FILE *out, int bin)
{
if (Debug) fprintf(stderr,"pjlSetOutBin called. bin=%i\n",bin);
/*
* Model Notes:
* 5SI This is only model supported
*/
if (NoPJL) return (LJ_EXIT_OK);
if (Model != LJ_MODEL_5SI) return (LJ_EXIT_OK);
if (bin == LJ_OUTBIN_TOP) {
if (fprintf(out,"@PJL SET OUTBIN=UPPER\n") < 0) {
return(LJ_WRITE_STREAM_ERR);
}
} else if (bin == LJ_OUTBIN_REAR ) {
if (fprintf(out,"@PJL SET OUTBIN=LOWER\n") < 0) {
return(LJ_WRITE_STREAM_ERR);
}
}
return (LJ_EXIT_OK);
}
/**************************************************************************/
int pjlSetTray(FILE *out, int tray)
{
/* Some printers set tray based on paper type; others can not.
* This is also used to put printer into manual mode.
*/
/*
* Model Notes:
* 4V Only MANUAL supported, let paper size dictate tray selection
* 4P Only MANUAL supported
* 4PLUS Only MANUAL supported (use PCL for other trays)
* 4SI Only MANUAL (use PCL for other trays)
* 5L Only MANUAL supported (no optional trays available)
* 5P Allows MP to be set as CASSETTE, FIRST, or MANUAL
* 5SI Only MANUAL, let paper size and media type dictate tray used
*/
if (Debug) fprintf(stderr,"pjlSetTray called. tray=%i\n",tray);
if (NoPJL) return (LJ_EXIT_OK);
if (tray == LJ_TRAY_MANUAL) {
if (fprintf(out,"@PJL SET MANUALFEED=ON\n") < 0) {
return(LJ_WRITE_STREAM_ERR);
}
if (Model == LJ_MODEL_5P) {
if (fprintf(out,"@PJL SET MPTRAY=MANUAL\n") < 0) {
return(LJ_WRITE_STREAM_ERR);
}
}
} else if (Model == LJ_MODEL_5P) {
if (Debug) fprintf(stderr,"pjlSetTray called model is 5P\n");
if (tray == LJ_TRAY_UPPER) { /* Paper Cassette / Tray 2 */
if (fprintf(out,"@PJL SET MPTRAY=CASSETTE\n") < 0) {
return(LJ_WRITE_STREAM_ERR);
}
} else if (tray == LJ_TRAY_LOWER) { /* MP tray / Tray 1 */
if (fprintf(out,"@PJL SET MPTRAY=CASSETTE\n") < 0) {
return(LJ_WRITE_STREAM_ERR);
}
} else if (tray == LJ_TRAY_EITHER) {
if (fprintf(out,"@PJL SET MPTRAY=FIRST\n") < 0) {
return(LJ_WRITE_STREAM_ERR);
}
}
}
return (LJ_EXIT_OK);
}
/**************************************************************************/
int pjlSetDarkness(FILE *out, int darkness)
{
int density=3;
/* Valid density is 1 to 5; we support normal, dark and light.
* an Economode setting is also supported.
*/
/*
* Model Notes:
* 4V Only ECONOMODE set; (set density on front panel)
* 4P ECONOMODE and DENSITY supported
* 4PLUS Only ECONOMODE set; (set density on front panel)
* 4SI Not supported
* 5L ECONOMODE and DENSITY supported
* 5P ECONOMODE and DENSITY supported
* 5SI Only ECONOMODE set; (set density on front panel)
*/
if (Debug) fprintf(stderr,"pjlSetDarkness called darkness=%i\n",darkness);
if (NoPJL) return (LJ_EXIT_OK);
if (Model == LJ_MODEL_4SI) return (LJ_EXIT_OK);
if (darkness == LJ_DARKNESS_ECONO) {
if (fprintf(out,"@PJL SET ECONOMODE=ON\n") < 0) {
return(LJ_WRITE_STREAM_ERR);
}
density = 3;
} else if (darkness == LJ_DARKNESS_LIGHT) {
density = 1;
} else if (darkness == LJ_DARKNESS_DARK) {
density = 5;
} else if (darkness == LJ_DARKNESS_NORMAL) {
density = 3;
}
if ((Model == LJ_MODEL_5P) || (Model == LJ_MODEL_4P)) {
if (Debug) fprintf(stderr,"pjlSetDarkness default density=%i\n",density);
if (fprintf(out,"@PJL DEFAULT DENSITY=%i\n",density) < 0) {
return(LJ_WRITE_STREAM_ERR);
}
} else if (Model == LJ_MODEL_5L) {
if (Debug) fprintf(stderr,"pjlSetDarkness set density=%i\n",density);
if (fprintf(out,"@PJL SET DENSITY=%i\n",density) < 0) {
return(LJ_WRITE_STREAM_ERR);
}
}
return (LJ_EXIT_OK);
}
/**************************************************************************/
void pjlSetDebug(boolean onoff)
{
/* Turn on debug nessages in the PJL and PCL functions in this file */
Debug = onoff;
}
/**************************************************************************/
int pclSetRes(FILE *out, int xres, int yres)
{
/* Set PCL raster graphics resolution. Settings from PJL are ignored
* so make sure you also set resolution in pcl. yres is ignored.
*/
if (Debug) {
fprintf(stderr,"pclSetRes called. xres=%i, yres=%i\n",xres,yres);
}
if (fprintf(out, "\033*t%dR", xres) < 0) return(LJ_WRITE_STREAM_ERR);
return (LJ_EXIT_OK);
}
/**************************************************************************/
int pclSetLineWrap(FILE *out)
{
/* Enable the end of line wrap in pcl */
if (Debug) fprintf(stderr,"pclSetLineWrap called\n");
if (fprintf(out,"\033&s0C") < 0) return(LJ_WRITE_STREAM_ERR);
return (LJ_EXIT_OK);
}
/**************************************************************************/
int pclSetCrLf(FILE *out)
{
/* Set CR/LF model to CR=CR; LF=CR-LF; FF=CR-FF*/
if (Debug) fprintf(stderr,"pclSetCrLf called\n");
if (fprintf(out,"\033&k2G") < 0) return(LJ_WRITE_STREAM_ERR);
return (LJ_EXIT_OK);
}
/**************************************************************************/
int pclFormFeed(FILE *out)
{
/* Send a form feed */
if (Debug) fprintf(stderr,"pclFormFeed called\n");
if (fprintf(out,"\014") < 0) return(LJ_WRITE_STREAM_ERR);
if (fflush(out) != 0) return (LJ_WRITE_STREAM_ERR);
return (LJ_EXIT_OK);
}
/**************************************************************************/
int pclResetPrinter(FILE *out)
{
/* Send a PCL reset */
if (Debug) fprintf(stderr,"pclResetPrinter called\n");
if (fprintf(out,"\033E") < 0) return(LJ_WRITE_STREAM_ERR);
return (LJ_EXIT_OK);
}
/**************************************************************************/
int pclSetTray(FILE *out, int tray)
{
/*
* Model Notes:
* 4V MANUAL and 2 trays (1 optional) supported
* 4PLUS MANUAL and 2 trays (1 optional) supported
* 4SI 2 Trays (standard with printer) supported
* 4P Only MANUAL supported (no optional trays available)
* 5L Only MANUAL supported (no optional trays available)
* 5P Allows selection between MP tray and paper drawer
* 5SI Not supported (paper size and type selects tray on 5SI)
*/
if (Debug) fprintf(stderr,"pclSetTray called. tray=%i\n",tray);
if (tray == LJ_TRAY_MANUAL) {
if (fprintf(out,"\033&l2H") < 0) {
return(LJ_WRITE_STREAM_ERR);
}
} else if ((Model == LJ_MODEL_5P) || (Model == LJ_MODEL_4SI) ||
(Model == LJ_MODEL_5SI)) {
if (Debug) fprintf(stderr,"pclSetTray called model is 5P/4SI/4V\n");
if (tray == LJ_TRAY_UPPER) { /* Paper Cassette / upper / Tray 2 */
if (fprintf(out,"\033&l1H") < 0) {
return(LJ_WRITE_STREAM_ERR);
}
} else if (tray == LJ_TRAY_LOWER) { /* MP tray / lower/ Tray 1 */
if (fprintf(out,"\033&l4H") < 0) {
return(LJ_WRITE_STREAM_ERR);
}
}
} else if ((Model == LJ_MODEL_4PLUS) || (Model == LJ_MODEL_4V)) {
if (tray == LJ_TRAY_UPPER) {
if (fprintf(out,"\033&l1H") < 0) {
return(LJ_WRITE_STREAM_ERR);
}
} else if (tray == LJ_TRAY_LOWER) {
if (fprintf(out,"\033&l5H") < 0) {
return(LJ_WRITE_STREAM_ERR);
}
}
}
return (LJ_EXIT_OK);
}
/**************************************************************************/
int pclPrepForGraphics(FILE *out)
{
/* Get ready for graphics mode. Calls several pcl functions.
* Specific to the laserjet driver dor Impressario in that some
* values are hard-coded.
*/
if (Debug) fprintf(stderr,"pclPrepForGraphics called\n");
/* Enter new page so mark compression used as unknown for pclSendRow */
CompressionSet = -1;
/* Set portrait mode on & zero top margin */
if (fprintf(out, "\033&l0ol0E") < 0) return(LJ_WRITE_STREAM_ERR);
/* Set zero left margin */
if (fprintf(out, "\033&l0L") < 0) return(LJ_WRITE_STREAM_ERR);
/* Move graphics cursor to 0,50 */
if (fprintf(out, "\033*p0x50Y") < 0) return(LJ_WRITE_STREAM_ERR);
/* Start raster graphics mode. */
if (fprintf(out, "\033*r1A") < 0) return(LJ_WRITE_STREAM_ERR);
InRasterMode = TRUE;
if (fflush(out) != 0) return (LJ_WRITE_STREAM_ERR);
return (LJ_EXIT_OK);
}
/**************************************************************************/
int pclPrepForNextPage(FILE *out)
{
if (Debug) fprintf(stderr,"pclPrepForNextPage called\n");
/* Get ready for next page */
/* End raster graphics */
if (InRasterMode) {
if (fprintf(out, "\033*rC") < 0) return(LJ_WRITE_STREAM_ERR);
InRasterMode = FALSE;
}
if (pclFormFeed(out) != LJ_EXIT_OK) return(LJ_WRITE_STREAM_ERR);
return (LJ_EXIT_OK);
}
/**************************************************************************/
int pclFlushPages(FILE *out)
{
if (Debug) fprintf(stderr,"pclFlushPages called\n");
if (fprintf(out, "\033&r1F") < 0) return(LJ_WRITE_STREAM_ERR);
if (fflush(out) != 0) return (LJ_WRITE_STREAM_ERR);
return (LJ_EXIT_OK);
}
/**************************************************************************/
int pclCleanupPrinter(FILE *out)
{
if (Debug) fprintf(stderr,"pclCleanupPrinter called\n");
/* End raster graphics */
if (InRasterMode) {
if (fprintf(out, "\033*rC") < 0) return(LJ_WRITE_STREAM_ERR);
InRasterMode = FALSE;
}
/* Send Job Separation command. */
if (fprintf(out, "\033&l1T") < 0) return(LJ_WRITE_STREAM_ERR);
if (pclResetPrinter(out) != LJ_EXIT_OK) return(LJ_WRITE_STREAM_ERR);
if (fprintf(out,"\033%%-12345X") < 0) return(LJ_WRITE_STREAM_ERR);
if (pjlEnterPJLMode(out) != LJ_EXIT_OK) return(LJ_WRITE_STREAM_ERR);
if (pjlResetPrinter(out) != LJ_EXIT_OK) return(LJ_WRITE_STREAM_ERR);
if (fflush(out) != 0) return (LJ_WRITE_STREAM_ERR);
return (LJ_EXIT_OK);
}
/**************************************************************************/
int packbits(unsigned char *ibits, unsigned char *pbits, int nbytes)
{
unsigned char *sptr;
unsigned char *ibitsend;
unsigned char *optr = pbits;
int todo, cc, count;
ibitsend = ibits+nbytes;
while(ibits<ibitsend) {
sptr = ibits;
ibits += 2;
while((ibits<ibitsend)&&((ibits[-2]!=ibits[-1])||(ibits[-1]!=ibits[0]))) ibits++;
if(ibits != ibitsend) {
ibits -= 2;
}
count = ibits-sptr;
while(count) {
todo = count>128 ? 128:count;
count -= todo;
*optr++ = todo-1;
while(todo--)
*optr++ = *sptr++;
}
if(ibits == ibitsend)
break;
sptr = ibits;
cc = *ibits++;
while( (ibits<ibitsend) && (*ibits == cc) )
ibits++;
count = ibits-sptr;
while(count) {
todo = count>128 ? 128:count;
count -= todo;
*optr++ = 257-todo;
*optr++ = cc;
}
}
return optr-pbits;
}
/**************************************************************************/
int set_compression_2(FILE *out)
{
if (fprintf(out,"\033*b2m") < 0) return(LJ_WRITE_STREAM_ERR);
CompressionSet=2;
return (LJ_EXIT_OK);
}
/**************************************************************************/
int set_compression_off(FILE *out)
{
if (fprintf(out,"\033*b0m") < 0) return(LJ_WRITE_STREAM_ERR);
CompressionSet=0;
return (LJ_EXIT_OK);
}
/**************************************************************************/
int pclSendRow(FILE *out, unsigned char *row, unsigned long numBytes)
{
/* Send a row of raster data -- compresses and then sends */
int bufSizeNeeded;
int bytesToWrite;
int compressLength;
int count;
int lineCount;
unsigned char *writeBuf;
unsigned char HeaderCmd[HEADER_LEN];
static int Buflength = 0;
static unsigned char *compressedRow = NULL;
int packbits(unsigned char *ibits, unsigned char *pbits, int nbytes);
int set_compression_off(FILE *out);
int set_compression_2(FILE *out);
bufSizeNeeded = numBytes;
if (Buflength < bufSizeNeeded) { /* alloc, re-alloc as needed */
if (Buflength == 0) { /* first time through */
Buflength = bufSizeNeeded;
compressedRow=(unsigned char *)malloc(Buflength*2);
} else {
Buflength = bufSizeNeeded;
compressedRow=(unsigned char *)realloc(compressedRow,Buflength*2);
}
}
if (!compressedRow) {
fprintf(stderr,"malloc failed while allocating bytes of memory");
exit(1);
}
bytesToWrite = Buflength;
writeBuf = row;
compressLength = packbits(row, compressedRow, Buflength);
if (compressLength < Buflength) {
bytesToWrite = compressLength;
writeBuf = compressedRow;
if (CompressionSet != 2) {
if (set_compression_2(out) != LJ_EXIT_OK) {
return (LJ_WRITE_STREAM_ERR);
}
}
lineCount = sprintf(HeaderCmd,"\033*b%uW",bytesToWrite);
} else {
if (CompressionSet != 0) {
if (set_compression_off(out) != LJ_EXIT_OK) {
return (LJ_WRITE_STREAM_ERR);
}
}
}
lineCount = sprintf(HeaderCmd,"\033*b%uW",bytesToWrite);
count = fwrite(HeaderCmd, 1, lineCount, out);
if (count != lineCount) return (LJ_WRITE_STREAM_ERR);
count = fwrite(writeBuf, 1, bytesToWrite, out);
if (count != bytesToWrite) return (LJ_WRITE_STREAM_ERR);
fflush(out);
return (LJ_EXIT_OK);
}
/**************************************************************************/